package cn.tedu.threadpool;

import java.util.concurrent.*;

public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 方式一：将Callable包装为Runnable来执行
        // Callable -> FutureTask -> RunnableFuture -> Runnable
        // FutureTask<String> task = new FutureTask<>(new CThread());
        // new Thread(task).start();
        // System.out.println(task.get());

        // 方式二：通过线程池来启动Callable线程
        ExecutorService es = new ThreadPoolExecutor(5, 10,
                5, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5));
        // 将Callable的执行结果封装成Future对象返回
        Future<String> f = es.submit(new CThread());
        es.shutdown();
        System.out.println(f.get());

    }

}

// 泛型定义的返回值类型
class CThread implements Callable<String> {
    @Override
    public String call() throws Exception {
        return "SUCCESS";
    }
}
